﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.AspNetCore.Structs
{
    public static class ModelTypeManager
    {
        private static List<ModelType> entityConfigs = new List<ModelType>();

        public static List<ModelType> ModelTypes
        {
            get { return entityConfigs; }
        }

        public static ModelType GetModelTypeByName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return null;
            }

            if (name.StartsWith("t_"))
            {
                name = "m_" + name.Substring(2);
            }

            return ModelTypes.FirstOrDefault(t => string.Compare(t.Name, name, true) == 0);
        }

        public static void AddModelType(ModelType modelType)
        {
            if (string.IsNullOrEmpty(modelType.Name))
            {
                return;
            }

            var old = ModelTypes.FirstOrDefault(t => string.Compare(t.Name, modelType.Name, true) == 0);
            if (old != null)
            {
                ModelTypes.Remove(old);
            }

            ModelTypes.Add(modelType);
        }
    }
}
